/*
 *  mini-SaaS
 *  Application for developing Software as a Service (SaaS) 
 *  Copyright (c) 2011 Anand Sudhakaran, Bryce Holton, Chao Zhang
 *  ASU
 *
 *  This file is part of mini-SaaS.
 *
 *  mini-SaaS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  mini-SaaS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with mini-SaaS; if not, see <http://www.gnu.org/licenses/>. 
 *
 */


/*
 *To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package loginbean;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author vexdan
 */
@WebServlet(name = "CreateWebService", urlPatterns = {"/CreateWebService"})
public class CreateWebService extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ResultSet rs;
        Statement st = null;
        Connection conn = null;

        int tenantId = 0;
        int objectId = 0;
        String webServiceName = request.getParameter("webServiceName");
        String url = request.getParameter("url");

        //String tenantEmail = request.getParameter("tenantEmail");
        //String tenantPassword = request.getParameter("tenantPassword");

        try {
            System.out.println("before classforname");
            conn = new DBManager().getDatabaseConnection();

            System.out.println("after classforname connection");

            HttpSession session = request.getSession(true);
            tenantId = Integer.parseInt(session.getAttribute("tenantid").toString());

            //insert into Object table
            String sql = "insert into mydb.webservicetable (WebServiceName,WebServiceURL,TenantID)"
                    + " VALUES(?,?,?)";

            PreparedStatement pst = conn.prepareStatement(sql);
            System.out.println(sql);
            pst.setString(1, webServiceName);
            pst.setString(2, url);
            pst.setInt(3, tenantId);

            int numRowsChanged = pst.executeUpdate();
            pst.close();


            if (numRowsChanged > 0) {
                //session.setAttribute("username",);	
                response.sendRedirect("welcome.jsp");
            } else {
                response.sendRedirect("error.jsp");
            }

        } catch (ClassNotFoundException e) {
            System.out.println("Couldn't load database driver: " + e.getMessage());
        } catch (SQLException e) {
            System.out.println("SQLException caught: " + e.getMessage());
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
}